define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/news/index',
                    add_url: '',
                    edit_url: 'jdy/news/edit',
                    del_url: 'jdy/news/del',
                    multi_url: 'jdy/news/multi',
                    table: 'jdy_news',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                showExport: false,
                dblClickToEdit: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'state', title: __('已讀'), defaultValue: 0, searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'type', title: __('Type'), searchList: {"註冊":__('註冊'),"工作提醒":__('工作提醒'),"裝置狀態":__('裝置狀態'),"保固到期":__('保固到期'),"其它":__('其它')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate:false, formatter: Controller.api.formatter.nameurl},
                        //{field: 'link', title: __('Link'), formatter: Table.api.formatter.url, operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //<a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a>
            $(".datetimerange").attr({'autocomplete': 'off'});

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                nameurl: function (value, row, index) {
                    if (row.task_id > 0) {
                        return '<a href="jdy/maintenance/add3/ids/' + row.task_id + '" class="btn btn-default btn-sm btn-dialog">'  + value + ' </a>';
                    } else {
                        return '<a href="' + row.link + '" target="_blank" class="btn btn-default btn-sm">'  + value + ' </a>';
                    }
                }
            }
        }
    };
    return Controller;
});